package com.test.fundible.fundible_00_utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.fundible.R;
import com.test.fundible.fundible_00_http.OkHttpUtil;

public class Photo_fundibleSelector_fundibleDialog extends Dialog {

    private final Runnable runnableByTakePhoto, runnableByFromAlbum;

    private View takePhotoClickView,fromAlbumClickView;
    private TextView takePhotoTv,fromAlbumTv;
    private View takePhotoCheckIv,fromAlbumCheckIv;
    private TextView confirmBtn;

    private boolean is_fundibleTake_fundiblePhoto = true;

    private final String TAG;

    public Photo_fundibleSelector_fundibleDialog(@NonNull Context context, Runnable takeAPhotoRunnable, Runnable fromAlbumRunnable,String tag) {
        super(context);
        this.runnableByTakePhoto = takeAPhotoRunnable;
        this.runnableByFromAlbum = fromAlbumRunnable;
        this.TAG = tag;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_fundible_photo_selector);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(true);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        findViewById(R.id.dfps_view_out).setOnClickListener(v -> {
            OkHttpUtil.getInstance_fundible().uploadEventPoint(TAG,"CLOSE_CLICK","");
            dismiss();
        });

        findViewById(R.id.dfps_view_in).setOnTouchListener((v, event) -> true);

        takePhotoClickView = findViewById(R.id.dfps_view_take_photo);
        fromAlbumClickView = findViewById(R.id.dfps_view_from_album_click);
        takePhotoTv = findViewById(R.id.dfps_tv_take_photo);
        fromAlbumTv = findViewById(R.id.dfps_tv_from_album);
        takePhotoCheckIv = findViewById(R.id.dfps_iv_take_photo_check);
        fromAlbumCheckIv = findViewById(R.id.dfps_iv_from_album_check);
        confirmBtn = findViewById(R.id.dfps_btn_confirm);

        init_fundibleView();
    }

    @SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
    private void init_fundibleView(){

        takePhotoClickView.setOnClickListener(v -> checkTakePhoto());
        fromAlbumClickView.setOnClickListener(v -> checkFromAlbum());

        confirmBtn.setOnClickListener(v -> {

            OkHttpUtil.getInstance_fundible().uploadEventPoint(TAG,"CONFIRM_CLICK","");

            if (is_fundibleTake_fundiblePhoto){
                if (runnableByTakePhoto !=null) runnableByTakePhoto.run();
                dismiss();
            }else {
                if (runnableByFromAlbum !=null) runnableByFromAlbum.run();
                dismiss();
            }
        });

    }

    private void checkTakePhoto(){
        is_fundibleTake_fundiblePhoto = true;

        OkHttpUtil.getInstance_fundible().uploadEventPoint(TAG,"TAKE_A_PHOTO_CLICK","");

        takePhotoClickView.setBackgroundResource(R.drawable.bg_green_ll_rc12dp_border_green_2dp);
        takePhotoTv.setTextColor(getContext().getColor(R.color.green_light));
        takePhotoCheckIv.setVisibility(View.VISIBLE);

        fromAlbumClickView.setBackgroundResource(R.drawable.bg_white_rc12dp);
        fromAlbumTv.setTextColor(Color.parseColor("#FF959695"));
        fromAlbumCheckIv.setVisibility(View.GONE);

    }

    private void checkFromAlbum(){
        is_fundibleTake_fundiblePhoto = false;

        OkHttpUtil.getInstance_fundible().uploadEventPoint(TAG,"UPLOAD_FROM_PHOTO_ALBUM_CLICK","");

        takePhotoClickView.setBackgroundResource(R.drawable.bg_white_rc12dp);
        takePhotoTv.setTextColor(Color.parseColor("#FF959695"));
        takePhotoCheckIv.setVisibility(View.GONE);

        fromAlbumClickView.setBackgroundResource(R.drawable.bg_green_ll_rc12dp_border_green_2dp);
        fromAlbumTv.setTextColor(getContext().getColor(R.color.green_light));
        fromAlbumCheckIv.setVisibility(View.VISIBLE);

    }

    @Override
    public void show() {
        super.show();
        OkHttpUtil.getInstance_fundible().uploadEventPoint(TAG,"END","");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        OkHttpUtil.getInstance_fundible().uploadEventPoint(TAG,"START","");
    }

}
